/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;

import Manager.TestConnection;
import Manager.Update.UpdateException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Uiii
 */
public class Barang {

    private String kodebarang;
    private String namabarang;
    private String Jenisbarang;
    private int Stock;
    private Double Hargabarang;

    public String getKodebarang() {
        return kodebarang;
    }

    public void setKodebarang(String kodebarang) throws UpdateException {
        if (kodebarang.matches("[0-9]{13}")) {
            this.kodebarang = kodebarang;
        } else {
            throw new UpdateException("Masukan kode 13 digit angka");
        }
    }

    public String getNamabarang() {
        return namabarang;
    }

    public void setNamabarang(String namabarang) {
        this.namabarang = namabarang;
    }

    public String getJenisbarang() {
        return Jenisbarang;
    }

    public void setJenisbarang(String Jenisbarang) {
        this.Jenisbarang = Jenisbarang;
    }

    public int getStock() {
        return Stock;
    }

    public void setStock(int Stock) {
        this.Stock = Stock;
    }

    public Double getHargabarang() {
        return Hargabarang;
    }

    public void setHargabarang(Double Hargabarang) {
        this.Hargabarang = Hargabarang;
    }

    public static int getStokR(String id) throws SQLException {
        String sql = "select STOK from BARANG where KODEBARANG = ?";
        PreparedStatement ps = new TestConnection().getConnection().prepareStatement(sql);
        ps.setString(1, id);
        ResultSet rs = ps.executeQuery();

        if (rs.next()) {
            return rs.getInt(1);
        } else {
            return -1;
        }
    }
}
